package umn.angrypenguins.monopoly;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import umn.angrypenguins.monopoly.models.GameInstance;
import umn.angrypenguins.monopoly.models.PlayerInstance;
import umn.angrypenguins.monopoly.webservice.WebServiceClient;

public class PlayerActivity extends Activity implements View.OnClickListener{
    private ArrayList<PlayerInstance> players;
    private GameInstance game;
    private PlayerInstance bank;
    private PlayerInstance player;

    class FindGameTask extends AsyncTask<String, String, GameInstance> {

        @Override
        protected GameInstance doInBackground(String... gameName) {
            return WebServiceClient.findGame( gameName[0] );
        }

        protected void onPostExecute(GameInstance gameInstance) {
            super.onPostExecute(gameInstance);

            game = gameInstance;
            players = game.getPlayers();
            refreshListOfOtherPlayers();
        }
    }

    class GetPlayerTask extends AsyncTask<Integer, Integer, PlayerInstance> {

        @Override
        protected PlayerInstance doInBackground(Integer... id) {
            return WebServiceClient.getPlayer( id[0] );
        }

        protected void onPostExecute(PlayerInstance playerInstance) {
            super.onPostExecute(playerInstance);
            
            player = playerInstance;
            ( (TextView)findViewById( R.id.main_user_amount ) ).setText( "$" + player.getWallet() );
            ((TextView)findViewById(R.id.main_user_label)).setText(player.getName() + ":");
        }
    }
    
    private void refreshListOfOtherPlayers() {
        players = game.getPlayers();
        
        if( bank == null ) {
            for( PlayerInstance bankSuspect: players ) {
                if( bankSuspect.getName().equals( "Bank" ) ) {
                    bank = bankSuspect;
                    break;
                }
            }
        }
        
        ListView listview = (ListView) findViewById(R.id.player_list);

        ArrayList<String> otherPlayers = new ArrayList<String>() {};
        for( PlayerInstance playerInGame: players )
        {
            if( playerInGame.getId() != player.getId() )
                if (!(playerInGame.getName().toString().equals(("Bank"))))
                otherPlayers.add(playerInGame.getName().toString() + ": $" + playerInGame.getWallet().toString());
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, otherPlayers);
        listview.setAdapter(adapter);
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);
        ToolbarConfig toolbarConfig = new ToolbarConfig(this, "Monopoly Money");
        toolbarConfig.getToolbarBackButton().setText( "Quit" );

        Bundle b = this.getIntent().getExtras();
        bank = (PlayerInstance)b.getSerializable( "bankInstance" );
        game = (GameInstance)b.getSerializable( "gameInstance" );
        player = (PlayerInstance) b.getSerializable("playerInstance");

        findViewById(R.id.player_tx_button).setOnClickListener(this);
        findViewById(R.id.player_rx_button).setOnClickListener(this);
        findViewById(R.id.toolbar_back_button).setOnClickListener(this);
        findViewById(R.id.player_refresh_button).setOnClickListener(this);
        
        ( (TextView)findViewById( R.id.main_user_title ) ).setText( game.getName() );

        new FindGameTask().execute( player.getGame() );
        new GetPlayerTask().execute( player.getId() );
    }

    public void onClick(View view) {
        if (view.getId() == R.id.player_tx_button) {
            Bundle bundle = new Bundle();
            bundle.putSerializable( "gameInstance", game );
            bundle.putSerializable("playerInstance", player);
            Intent intent = new Intent();
            intent.setClass(this, TransferActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
        else if (view.getId() == R.id.player_rx_button) {
            Bundle bundle = new Bundle();
            bundle.putSerializable( "bankInstance", bank );
            bundle.putSerializable( "gameInstance", game );
            bundle.putSerializable("playerInstance", player);
            Intent intent = new Intent();
            intent.setClass(this, CollectActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
        else if (view.getId() == R.id.toolbar_back_button) {
            Intent intent = new Intent(this, GameView.class);
            startActivity(intent);
        }
        else if (view.getId() == R.id.player_refresh_button) {
            new FindGameTask().execute( player.getGame() );
            new GetPlayerTask().execute( player.getId() );
        }
    }
}
